using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TVT.Common.Exceptions;
using System.Threading;
using System.Collections.Generic;
using TVT.Common.Helpers;
using TVT.UI.Helpers;
using TVT.BizLayer.Entities;
using System.IO;

namespace TVT.UI.UserControls
{
    public partial class UserProfileControl : System.Web.UI.UserControl
    {
        public Guid guid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadUserData((Guid)Membership.GetUser().ProviderUserKey, true);
            }
        }

        public void LoadUserData(Guid userId, bool isLoggedUser)
        { 
            User user = User.Find(userId);
            lblUserNameTxt.Text = user.FirstName + " " + user.LastName;
            lblHighTxt.Text = user.UserProfile.High + " cm";
            lblWeightTxt.Text = user.UserProfile.Weight + " Kg";
            lblSexTxt.Text = (user.UserProfile.Male) ? "Male" : "Female";
            lblLookingForTxt.Text = user.UserProfile.Likes.ToString();
            lblEmailTxt.Text = user.Email;
            String imgUrl = ConfigurationManager.AppSettings[WellKnownKeys.TemporaryFilesPathKey].ToString() + user.UserName + ".jpg";
            String fullPath = imgUrl.Replace("~", AppDomain.CurrentDomain.BaseDirectory);
            if (File.Exists(fullPath))
                this.imgUser.ImageUrl = imgUrl;

            lblDataUpload.Visible = isLoggedUser;
            fuPhoto.Visible = isLoggedUser;
            lbtnUploadFile.Visible = isLoggedUser;

            txtMessage.Visible = !isLoggedUser;
            btnSendMessage.Visible = !isLoggedUser;
        }

        public void SetUser(Guid userId)
        {
            guid = userId;
            ViewState[WellKnownKeys.IdUserKey] = userId;
            LoadUserData(userId, false);
            this.upUserProfile.Update();
        }

        public void SendMessage(object sender, EventArgs args)
        {
            if (txtMessage.Text != String.Empty)
            {
                Guid userFrom = (Guid)Membership.GetUser().ProviderUserKey;
                Guid userTo = (Guid)ViewState[WellKnownKeys.IdUserKey];
                Comment comment = new Comment() { UserIdFrom = userFrom, UserIdTo = userTo, Text = txtMessage.Text };
                comment.Save();
                txtMessage.Text = String.Empty;
            }
        }

        protected void UploadFile(object sender, EventArgs e)
        {
            lblTransactionResultMessage.Visible = true;

            try
            {
                if (fuPhoto.HasFile)
                {
                    String userName = User.Find((Guid)Membership.GetUser().ProviderUserKey).UserName;
                    String ext = ".jpg";
                    string file = ConfigurationManager.AppSettings[WellKnownKeys.TemporaryFilesPathKey].ToString() + userName + ext;
                    String fullPath = file.Replace("~", AppDomain.CurrentDomain.BaseDirectory);
                    int count = 1;
                    while (File.Exists(fullPath))
                    {
                        fullPath = file.Substring(0, fullPath.Length - ext.Length) + count + ext;
                        count++;
                    }
                    fuPhoto.SaveAs(fullPath);
                    this.imgUser.ImageUrl = file;
                    lblTransactionResultMessage.Text = Resources.SuccesfullMessage;
                }
            }
            catch (InvalidFileExtensionException ex)
            {
                lblTransactionResultMessage.Text = Resources.InvalidFile.Replace(TVT.Common.Helpers.WellKnownKeys.FileExtensionResourceReplace, ex.CorrectExtension);
                ExceptionLogger.HandleException(ex);
            }
            catch (Exception ex)
            {
                lblTransactionResultMessage.Text = Resources.ErrorMessage;
                ExceptionLogger.HandleException(ex);
            }
        }

        public void EraseMessage()
        {
            lblTransactionResultMessage.Visible = false;
        }
    }
}